package cn.lg.soar.general.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.general.api.constant.ModuleConst;
import cn.lg.soar.general.config.FileProps;
import cn.lg.soar.general.entity.FileMetadata;
import cn.lg.soar.general.model.ConfigVo;
import cn.lg.soar.general.api.model.FileDigestVO;
import cn.lg.soar.general.api.model.FileVO;
import cn.lg.soar.general.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Api(tags = "文件上传控制")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/file")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private FileProps props;

    @ApiOperation("秒传")
    @PostMapping(path = "fastUpload", name = "秒传")
    public HttpResult<List<FileVO>> fastUpload(@ApiParam @RequestBody Collection<FileDigestVO> vos) {
        List<FileMetadata> metadataList = fileUploadService.fastUpload(vos);
        return HttpResult.data(fileUploadService.toVos(metadataList));
    }

    @ApiOperation("上传")
    @PostMapping(path = "upload", name = "上传")
    public HttpResult<List<FileVO>> upload(@ApiParam @RequestParam("files") MultipartFile[] files, @ApiParam String bucketName) {
        if (DataUtil.isValuable(bucketName)) {
            List<FileMetadata> metadataList = fileUploadService.upload(bucketName, files);
            return HttpResult.data(fileUploadService.toVos(metadataList));
        }
        List<FileMetadata> metadataList = fileUploadService.upload(files);
        return HttpResult.data(fileUploadService.toVos(metadataList));
    }

    @ApiOperation("获取配置")
    @GetMapping(path = "config", name = "获取配置")
    public HttpResult<ConfigVo> config() {
        ConfigVo configVo = new ConfigVo();
        configVo.setBlacklist(props.getBlacklist());
        configVo.setWhitelist(props.getWhitelist());
        return HttpResult.data(configVo);
    }

}
